function goodsDetail(id){
    var p=new Promise(function(resolve,reject){
        $.ajax({
            type: "get",
            url: "../php/goodDetail.php",
            data: {
                goods_id:id
            },
            dataType: "json",
            success: function(data) {
                resolve(data)
            }
        })
    })
    return p
}
function addCar(user_id,user,good_id,goods_title,goods_num,goods_current_price,goods_pic){
    var p=new Promise(function(resolve,reject){
            $.ajax({data:{
                    user_id:user_id,
                    user_name:user,
                    goods_id:good_id,
                    goods_name:goods_title,
                    goods_num:goods_num,
                    goods_price:goods_current_price,
                    goods_pic:goods_pic
                },
            type: "get",  
            url:"../php/addtocar.php",
            dataType:"json",
            success:function(e){
                    resolve(e)
            }})
            })
            return p   
}
    var e1
    if(location.search){
    var a=location.search.split("&")
    var id=a[0].split("=")[1]
    var menu=a[1].split("=")[1]
    menu=decodeURI(menu)
    // console.log(id,menu)
    $(".small").html(`首页 > ${menu}`)
    }
    
    goodsDetail(id).then(function(e){
        console.log(e)
        e1=e
        $src_sub1=e.goods_sub_img.split(",")[0]
        $src_sub2=e.goods_sub_img.split(",")[1]
        $src_sub3=e.goods_sub_img.split(",")[2]
        $src_sub4=e.goods_sub_img.split(",")[3]
        $(".big img").attr("src",$src_sub1)
        $(".big2").css({
            display:"none"
        })
        $(".small_img ul").html(
            `<li p_id=1><img src="${$src_sub1}" alt=""></li>
            <li p_id=2><img src="${$src_sub2}" alt="" ></li>
            <li p_id=3><img src="${$src_sub3}" alt="" ></li>
            <li p_id=4><img src="${$src_sub4}" alt="" ></li>`
        )
        $(".big").mouseout(function(){
            $(".shadow_1").css({
               display:"none",
           })
           $(".big2").css({
               display:"none",
           })
        })
        $(".big_wrap").mousemove(function(e){
            $(".big2").css({
                display:"block"
            })
            var e=e||window.event;
            var x=e.pageX-parseInt($(".big").offset().left)-$(".shadow_1").outerWidth()/2
            var y=e.pageY-parseInt($(".big").offset().top)-$(".shadow_1").outerHeight()/2
            var maxX=$(".big").outerWidth()-$(".shadow_1").outerWidth()
            var maxY=$(".big").outerHeight()-$(".shadow_1").outerHeight()
            // console.log(e.pageX,parseInt($(".big").offset().left),$(".shadow_1").outerWidth()/2)
            if(x<=0){
                x=0;
            }
            if(y<=0){
                y=0;
            }
            if(x>=maxX){
                x=maxX;
            }
            if(y>=maxY){
                y=maxY;
            }
           $(".shadow_1").css({
               display:"block",
               left:x,
               top:y
           })
           $(".big2 li img").css({
               display:"block",
               left:-3*x,
               top:-3*y
           })
        })
        $(".small_img ul li").hover(function(){
            $(this).addClass("active").siblings().removeClass("active")
            id=$(this).attr("p_id")
            $(".big img").attr("src",`../img/goodlist_img_sub/pic_sub${id}.jpg`)
            $(".big2").css({
                display:"none"
            })
        },function(){
            $(".small_img ul li").removeClass("active")
            // id=a[0].split("=")[1]
        //     $(".big img").attr("src",`../img/goodlist_img_sub/pic${id}.jpg`)
        //     $(".big2").css({
        //      display:"none"
        //    })
        })
      $(".info h1").html(e.goods_title)
      $(".info .price").html(e.goods_price)
      $(".info .detail").html(e.goods_detail)
    })

$(".reduce").click(function(){
    if($(".count input").val()>1){
        $num=Number($(".count input").val())
        $num--
        $(".count input").val($num--)
    }
})
$(".add").click(function(){
    $num=Number($(".count input").val())
    $num++
    $(".count input").val($num) 
})
$(".shopping").click(function(){
    var user=localStorage.getItem("user")
    var pwd=localStorage.getItem("pwd")
    var user_id=localStorage.getItem("user_id")
    if(user&&pwd&&user_id){
        var num=$(".count input").val()
        addCar(user_id,user,e1.goods_id,e1.goods_title,num,e1.goods_price,e1.goods_img).then(
            function(result){
                if(result.status){
                    if(confirm("加入购物车成功，是否跳转购物车")){
                        location.href ="../html/shoppingcar.html?user_id="+user_id
                    }
                }
            }
        )
    }
    else{
        localStorage.setItem("backurl",location.href)
        location.href="../html/login.html"
    }
})
$(".buy").click(function(){
    var user=localStorage.getItem("user")
    var pwd=localStorage.getItem("pwd")
    var user_id=localStorage.getItem("user_id")
    if(user&&pwd&&user_id){
        var num=$(".count input").val()
        location.href ="../html/shoppingcar.html?user_id="+user_id+"&num="+num
    }
    else{
        localStorage.setItem("backurl",location.href)
        location.href="../html/login.html" 
    }
})


